﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using DAL;

namespace 包覆门.HingeManager
{
    public partial class HingeEditing : Form
    {
        public HingeEditing()
        {
            InitializeComponent();
        }
        public string EditingId { get; set; }
        public bool IsNew { get; set; }
        private HingeDAL dal = new HingeDAL();
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            Hinge h = new Hinge();
            if (!ValidateData()) return;
            h.Hbrand = this.tbbrand.Text.Trim();
            h.Hname = this.tbhname.Text.Trim();
            h.Hprice = Convert.ToDecimal(this.tbprice.Text.Trim());
            if (IsNew)
            {
                int result = dal.SaveData(h);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                    
                }
                else {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else {
                h.Id = Convert.ToInt32(EditingId);
                int result = dal.UpdateData(h);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;

                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }

        private void HingeEditing_Load(object sender, EventArgs e)
        {
            if (IsNew) { } else {
                Hinge h = dal.GetHingeById(EditingId);
                this.tbbrand.Text = h.Hbrand;
                this.tbhname.Text = h.Hname;
                this.tbprice.Text = h.Hprice.ToString();
            }
        }
        public bool ValidateData() { 
            bool flag =true;
            if(string.IsNullOrWhiteSpace(this.tbbrand.Text)){
                flag = false;
                MessageBox.Show("品牌名称不能为空");
            }
            if (string.IsNullOrWhiteSpace(this.tbhname.Text))
            {
                flag = false;
                MessageBox.Show("铰链名称不能为空");
            }
            if (string.IsNullOrWhiteSpace(this.tbprice.Text))
            {
                flag = false;
                MessageBox.Show("价格不能为空");
            }
            return flag;
        }
        //只能是数字
        private void tbprice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }
    }
}
